﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace MasterPageGroupF3.AdminPages
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        private int flag;
        private COMMENT comt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDCOMMENT"] != null && IsPostBack == false)
            {
                ListItem l = new ListItem();
                comt = COMMENTBUS.layCommentByID((int)Session["IDCOMMENT"]);
                l.Text = comt.USERNAME;
                DDUsername.Items.Add(l);
                ListItem l1 = new ListItem();
                DDUsername.Enabled = false;
                String content = comt.PRODUCT.SLOGAN;
                l1.Text = (content.Length > 25) ? content.Substring(0, 25) : content;
                DDProduct.Items.Add(l1);

                DDProduct.Enabled = false;
                tbComment.Text = comt.CONTENT;
                flag = 0;
                Page.Title = "Chỉnh sửa bình luận";
            }
            if (Session["IDCOMMENT"] == null)
            {
                Page.Title = "Thêm bình luận mới";
                List<PRODUCT> comment = PRODUCTBUS.LayBang().ToList();
                ListItem l = new ListItem();
                l.Text = "Administrator";
                l.Value = "Administrator";
                DDUsername.Items.Add(l);
                ListItem l1 = new ListItem();
                l1.Text = "Moderator";
                l1.Value = "Moderator";
                DDUsername.Items.Add(l1);
                flag = 1;
                for (int i = 0; i < comment.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = (comment[i].SLOGAN.Length > 25) ? comment[i].SLOGAN.Substring(0, 25) : comment[i].SLOGAN;
                    item.Value = comment[i].IDPRODUCT.ToString();
                    DDProduct.Items.Add(item);
                }
            }
            
        }

        protected void Apply_Click(object sender, ImageClickEventArgs e)
        {
            if (flag == 1)
            {
                COMMENT cm = new COMMENT();
                cm.USERNAME = DDUsername.SelectedValue;
                cm.IDPRODUCT = Int32.Parse(DDProduct.SelectedValue);
                cm.CONTENT = tbComment.Text;
                cm.TIME = DateTime.Today;
                COMMENTBUS.Add_Comment(cm);
                Response.Redirect("ManageComments.aspx");
            }
            else
            {
                COMMENT cm = new COMMENT();
                cm.IDCOMMENT = (int)Session["IDCOMMENT"];
                cm.CONTENT = tbComment.Text;
                cm.TIME = DateTime.Today;
                COMMENTBUS.updateComment(cm);
                Session.Remove("IDCOMMENT");
                Response.Redirect("ManageComments.aspx");
            }
        }

        protected void Cancel_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["IDCOMMENT"] != null)
                Session.Remove("IDCOMMENT");
            Response.Redirect("ManageComments.aspx");
        }
    }
}